TARGET = ./target

TOOLCHAIN = aarch64-elf-12.1.0-Darwin-arm64

# TOOLCHAIN = aarch64-elf-11.3.0-Linux-aarch64

# lzw
GCC = ./toolchain/$(TOOLCHAIN)/bin/aarch64-elf-gcc
LD = ./toolchain/$(TOOLCHAIN)/bin/aarch64-elf-ld
AS = ./toolchain/$(TOOLCHAIN)/bin/aarch64-elf-as

# yang
# GCC = /Users/neroyang/aarch64-unknown-elf/bin/aarch64-unknown-elf-gcc
# LD = /Users/neroyang/aarch64-unknown-elf/bin/aarch64-unknown-elf-ld
# AS = /Users/neroyang/aarch64-unknown-elf/bin/aarch64-unknown-elf-as

CFLAGS = -mcpu=cortex-a72+nofp -mlittle-endian -fno-builtin -nostdlib -g -O0
LDFLAGS = -static -nostdlib -g -O0

#objects =  $(TARGET)printf.o $(TARGET)loader.o $(TARGET)kernel.o $(TARGET)serial.o

BUILD_DIR = $(wildcard ./*.c ./*/*.c ./*/*/*.c ./*.s)
OBJ_DIR = $(patsubst %.s, %.o, $(patsubst %.c, %.o, $(BUILD_DIR)))

LIB = -I.
    
test:
	echo $(OBJ_DIR)

QEMU = qemu-system-aarch64


%.o: %.c
	$(GCC) $(CFLAGS) $(LIB) -o $(TARGET)/$(notdir $*.o) -c $<

%.o: %.s
	$(GCC) $(CFLAGS) $(LIB)  -o $(TARGET)/$(notdir $*.o) -c $<

all: clean mykernel.elf

mykernel.elf: $(OBJ_DIR)
	$(LD) $(LDFLAGS) -T ./linker.ld -o $(TARGET)/$@ $(foreach dir, $(notdir $^),$(TARGET)/$(dir))


qemu: all
	$(QEMU) \
	-machine virt \
	-cpu cortex-a72 \
	-vga none -device ramfb \
	-smp cores=1 \
	-m 512 \
	-kernel $(TARGET)/mykernel.elf \
	-serial mon:stdio \
	# -nographic \
	# -gdb tcp::1234 -S


clean:
	rm -rf $(TARGET)/*.o
	rm -rf $(TARGET)/*.elf
